local texture = {}
local Anim = require("lib/animation")

function texture:init()
	love.graphics.setBackgroundColor(0.3,0.3,0.3)
    love.graphics.setDefaultFilter("nearest","nearest")
    self.font = love.graphics.setNewFont("res/cn2.ttf",36/GlobalScale)
    self.midfont = love.graphics.setNewFont("res/cn2.ttf",24/GlobalScale)
    self.tinyfont = love.graphics.setNewFont("res/cn2.ttf",12/GlobalScale)
	self:cutColor()
	self.quad = {}
	self:initTank()
	self:initEffect()
	self:initItem()
	self:initTile()
	self.colorPal = {}
	return self
end
---231,231,148 -- 0.90588235294118	0.90588235294118	0.58039215686275 亮
---107,107,0 -- 0.41960784313725	0.41960784313725	0  暗
---231,156,33 -- 0.90588235294118	0.61176470588235	0.12941176470588 正

---0.4 0.4 0 
---0.9 0.9 0.6
---0.9 0.6 0.1
function texture:cutColor()
	local imagedata = love.image.newImageData("res/tiles.png")
	imagedata:mapPixel(function(x,y,r,g,b,a)
	    if r == 0 and g == 0 and b <= 0.01 then
	        b = 0
	        a = 0
	    end
	    return r,g,b,a
	end)
	self.image = love.graphics.newImage(imagedata)
	self.imageW,self.imageH = self.image:getDimensions()
end

function texture:initTank()
	local tankAnimQuad = {{},{},{},{}}
	for i = 0,7 do
	    tankAnimQuad[1][i+1] = {0,i*16,16,16,0,0,2,1,0.1}
	    tankAnimQuad[2][i+1] = {128,i*16,16,16,0,0,2,1,0.1}
	    tankAnimQuad[3][i+1] = {0,i*16+128,16,16,0,0,2,1,0.1}
	    tankAnimQuad[4][i+1] = {128,i*16+128,16,16,0,0,2,1,0.1}
	end
	self.quad.tank = tankAnimQuad
end

function texture:newTankAnim(team,type)
	team = team%4
    if team == 0 then team  = 4 end
	return Anim(self.image,unpack(self.quad.tank[team][type]))
end

function texture:initItem()
	local itemQuad = {}
	for i = 0,6 do
		itemQuad[i+1] = love.graphics.newQuad(255+16*i, 111, 16, 16, 400, 256)
	end
	self.quad.item = itemQuad

end

function texture:initEffect()
	self.quad.star = {256,95,16,16,0,0,4,1,0.1}
	self.quad.shell = {256,143,16,16,0,0,2,1,0.05,2}
	self.quad.ding = {255,127,16,16,0,0,3,1,0.1,3}
	self.quad.bullet = love.graphics.newQuad(323,102,3,4,400,256)
	self.quad.hq = love.graphics.newQuad(304, 32, 16, 16, 400, 255) 
	self.quad.fallen = love.graphics.newQuad(304+16, 32, 16, 16, 400, 256) 
end

function texture:initTile()
	local tileQuad = {
	    love.graphics.newQuad(264,72,8,8,400,256),
	    love.graphics.newQuad(256,64,8,8,400,256),
	    love.graphics.newQuad(256,72,8,8,400,256),
	    love.graphics.newQuad(256,80,8,8,400,256),
	    love.graphics.newQuad(272,72,8,8,400,256),
	    love.graphics.newQuad(272,88,8,8,400,256),
	    love.graphics.newQuad(308,64,8,8,400,256),
	}
	self.quad.tile = tileQuad
end


function texture:newAnim(name)
	return Anim(self.image,unpack(self.quad[name]))
end

function texture:drawItem(type,...)
	love.graphics.draw(self.image,self.quad.item[type],...)
end

function texture:draw(name,...)
	love.graphics.draw(self.image,self.quad[name],...)
end

function texture:generateBatch()

end

function texture:drawTile(type,...)
	love.graphics.draw(self.image,self.quad.tile[type],...)
end

function texture:testInit()
	self.test = {}
	self.test.tank = {}
	for i = 1,4 do
		self.test.tank[i] = {}
		for j = 1,8 do
			self.test.tank[i][j] = Anim(self.image,unpack(self.quad.tank[i][j]))
		end
	end
end

function texture:testDraw()
	love.graphics.setColor(0, 0, 0, 1)
	love.graphics.rectangle("fill", 1,1,600,600)
	love.graphics.setColor(1, 1, 1, 1)
	for i = 1,4 do
		for j = 1,8 do
			local anim = self.test.tank[i][j]
			anim:update(0.017)
			anim:draw(j*60,i*100,0,4,4)
		end
	end
end


return texture